package com.example.lyl.yuejian.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lyl.yuejian.BuildConfig;
import com.example.lyl.yuejian.R;
import com.example.lyl.yuejian.bean.PhotoLvBean;
import com.example.lyl.yuejian.bean.ResultBean;
import com.example.lyl.yuejian.net.NetTool;
import com.example.lyl.yuejian.util.SharePreferenceUtils;
import com.example.lyl.yuejian.view.my.MyPhotoActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by dllo on 18/3/25.
 */

public class PhotoLvAdapter extends BaseAdapter {
    PhotoLvBean photoLvBean;

    private PlBtnClickListener listener;

    public void setListener(PlBtnClickListener listener) {
        this.listener = listener;
    }

    boolean flag;
    boolean plflag;

    public void setPlflag(boolean plflag) {
        this.plflag = plflag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void setPhotoLvBean(PhotoLvBean photoLvBean) {
        this.photoLvBean = photoLvBean;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (photoLvBean == null) {
            return 0;
        } else {
            return photoLvBean.getData().size();
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photolv, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (flag) {
            holder.deleteBtn.setVisibility(View.VISIBLE);
        }
        if (plflag) {
            holder.plBtn.setVisibility(View.VISIBLE);
        }
        Glide.with(parent.getContext()).load("http://" + photoLvBean.getData().get(position).getUser().getUpic()).placeholder(R.drawable.peopleicon).into(holder.yhtxCiv);
        holder.yhmTv.setText(photoLvBean.getData().get(position).getUser().getUname());
        holder.nrTv.setText(photoLvBean.getData().get(position).getPname());
        Glide.with(parent.getContext()).load("http://" + photoLvBean.getData().get(position).getPic()).placeholder(R.mipmap.lunbo3).into(holder.nrIv);
        holder.sjTv.setText(photoLvBean.getData().get(position).getPtime());
        StringBuffer sb = new StringBuffer();
        if (photoLvBean.getData().get(position).getCommentList().size() == 0) {
            holder.plTv.setVisibility(View.GONE);
        } else {
            holder.plTv.setVisibility(View.VISIBLE);
            for (int i = 0; i < photoLvBean.getData().get(position).getCommentList().size(); i++) {
                if (i == photoLvBean.getData().get(position).getCommentList().size() - 1) {
                    sb.append("评论" + (i + 1) + ":  " + photoLvBean.getData().get(position).getCommentList().get(i).getComcontent());
                } else {
                    sb.append("评论" + (i + 1) + ":  " + photoLvBean.getData().get(position).getCommentList().get(i).getComcontent() + "\n");
                }
            }
        }
        holder.plTv.setText(sb);

        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NetTool.getInstance().getApi()
                        .deleteMyPhoto(photoLvBean.getData().get(position).getPid())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<ResultBean>() {
                            @Override
                            public void accept(ResultBean resultBean) throws Exception {
                                NetTool.getInstance().getApi()
                                        .myPhotoRequest(SharePreferenceUtils.getInt(parent.getContext(), "uid", 0))
                                        .subscribeOn(Schedulers.io())
                                        .observeOn(AndroidSchedulers.mainThread())
                                        .subscribe(new Consumer<PhotoLvBean>() {
                                            @Override
                                            public void accept(PhotoLvBean photoLvBean) throws Exception {
                                                setPhotoLvBean(photoLvBean);
                                            }
                                        }, new Consumer<Throwable>() {
                                            @Override
                                            public void accept(Throwable throwable) throws Exception {
                                                Toast.makeText(parent.getContext(), "网络异常", Toast.LENGTH_SHORT).show();
                                            }
                                        });
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                Toast.makeText(parent.getContext(), "检查网络", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });

        holder.plBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.clickListener(photoLvBean.getData().get(position).getPid());
            }
        });
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.yhtx_civ)
        CircleImageView yhtxCiv;
        @BindView(R.id.yhm_tv)
        TextView yhmTv;
        @BindView(R.id.nr_tv)
        TextView nrTv;
        @BindView(R.id.nr_iv)
        ImageView nrIv;
        @BindView(R.id.sj_tv)
        TextView sjTv;
        @BindView(R.id.pl_tv)
        TextView plTv;
        @BindView(R.id.delete_btn)
        Button deleteBtn;
        @BindView(R.id.pl_btn)
        Button plBtn;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface PlBtnClickListener {
        void clickListener(int pid);
    }
}
